package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jt.mapper.ItemMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired(required = false)
    private ItemMapper itemMapper;
    @Autowired(required = false)
    private ItemDescMapper itemDescMapper;

    /**
     * 分页Sql:
     * 查询第一页:
     * select * from tb_item limit 起始位置,查询条数
     * select * from tb_item limit 0,20    共21个数  index[0,19]
     * 查询第二页:
     * select * from tb_item limit 20,20    index[20,39]
     * 查询第三页:
     * select * from tb_item limit 40,20
     * 查询第N页:
     * select * from tb_item limit (page-1)rows,20
     *
     * @param page
     * @param rows
     */
    @Override
    public EasyUITable findItemByPage(int page, int rows) {
        /**
         * 1.手写分页
         * long total = itemMapper.selectCount(null);
         * int startIndex = (page - 1) * rows;
         * List<Item> itemList = itemMapper.findItemByPage(startIndex, rows);
         */
        //2.利用MP方式实现分页
        IPage mpPage = new Page(page, rows);
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("updated");
        mpPage = itemMapper.selectPage(mpPage, queryWrapper);
        long total = mpPage.getTotal(); /* 获取记录总数 */
        List<Item> itemList = mpPage.getRecords(); /* 获取查询当前页 */
        return new EasyUITable(total, itemList);
    }

    /**
     * @param item
     * @param itemDesc 重构新增
     */
    @Override
    @Transactional /* 开启事务 */
    public void saveItem(Item item, ItemDesc itemDesc) {
        //Date date = new Date();
        //item.setStatus(1).setCreated(date).setUpdated(date);
        item.setStatus(1);
        /* MP: 如果完成主键自增 则自动实现数据得回显 */
        itemMapper.insert(item);
        /* 获取主键信息 */
        itemDesc.setItemId(item.getId());
        itemDescMapper.insert(itemDesc);
    }

    @Override
    @Transactional /* 开启事务 */
    public void updateItem(Item item, ItemDesc itemDesc) {
        itemMapper.updateById(item);
        /* 补全数据 */
        itemDesc.setItemId(item.getId());
        itemDescMapper.updateById(itemDesc);
    }

    @Override
    @Transactional /* 开启事务 */
    public void deleteItems(Long[] ids) {
        List<Long> listIds = Arrays.asList(ids);
        itemMapper.deleteBatchIds(listIds);
    }

    /* 参数说明:  entity:修改数据的值 updateWrapper */
    @Override
    @Transactional /* 开启事务 */
    public void updateStatus(Long[] ids, Integer status) {
        Item item = new Item();
        item.setStatus(status);
        /* where id in (1,2,3,4) */
        UpdateWrapper<Item> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", Arrays.asList(ids));
        itemMapper.update(item, updateWrapper);
    }

    /**
     * @param itemId
     * @return ItemDesc
     */
    @Override
    public ItemDesc findItemDescById(Long itemId) {
        return itemDescMapper.selectById(itemId);
    }

}
